package com.example.client.Utiles;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Calendar;
import java.util.Date;
import java.util.Map;

public class JWTUtiles {

    private static final Long EXPIRE = 1000*60*60*24L;

    private static final String SING = "!@#$%^&*()";

    public static String getToken(Map<String, String> map) {

        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.HOUR, 10);

        JWTCreator.Builder builder = JWT.create();
        map.forEach((k, v)->{
            builder.withClaim(k,v);
        });
        String token = builder.withExpiresAt(new Date(System.currentTimeMillis() + EXPIRE))
                .sign(Algorithm.HMAC256(SING));

        return token;
    }

    public static void vertify(String token) {
        JWT.require(Algorithm.HMAC256(SING)).build().verify(token);
    }

    public static DecodedJWT vertifyForData(String token) {
        return JWT.require(Algorithm.HMAC256(SING)).build().verify(token);
    }

}
